package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveMetaFieldDetailSvc;
import com.kmxd.ams.client.archive.dto.ArchiveMetaFieldDetailAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveMetaFieldDetailEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveMetaFieldDetailQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveMetaFieldDetailListVO;
import com.kmxd.ams.client.archive.vo.ArchiveMetaFieldDetailVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveMetaFieldDetail;
import com.kmxd.ams.infra.archive.mapper.ArchiveMetaFieldDetailMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 元数据对应明细 服务实现类
 *
 * @author nickbi
 * @since 2022-07-23
 */
@Service
@AllArgsConstructor
public class ArchiveMetaFieldDetailSvcImpl
    extends ServiceImpl<ArchiveMetaFieldDetailMapper, ArchiveMetaFieldDetail>
    implements IArchiveMetaFieldDetailSvc {

  private ArchiveMetaFieldDetailMapper archiveMetaFieldDetailMapper;

  @Override
  public Page<ArchiveMetaFieldDetailListVO> selectPage(
      Page reqPage, ArchiveMetaFieldDetailQueryDTO req) {
    LambdaQueryWrapper<ArchiveMetaFieldDetail> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveMetaFieldDetail>(), req).lambda();
    Page<ArchiveMetaFieldDetail> page =
        archiveMetaFieldDetailMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveMetaFieldDetailListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveMetaFieldDetailListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveMetaFieldDetailListVO> selectList(ArchiveMetaFieldDetailQueryDTO req) {
    LambdaQueryWrapper<ArchiveMetaFieldDetail> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveMetaFieldDetail>(), req).lambda();
    List<ArchiveMetaFieldDetail> list = archiveMetaFieldDetailMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveMetaFieldDetailListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveMetaFieldDetailAddDTO req) {
    ArchiveMetaFieldDetail entity = BeanUtil.copyProperties(req, ArchiveMetaFieldDetail.class);
    archiveMetaFieldDetailMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveMetaFieldDetailEditDTO req) {
    ArchiveMetaFieldDetail entity = BeanUtil.copyProperties(req, ArchiveMetaFieldDetail.class);
    archiveMetaFieldDetailMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveMetaFieldDetailVO view(Long id) {
    ArchiveMetaFieldDetail entity = archiveMetaFieldDetailMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveMetaFieldDetailVO.class);
  }
}
